import swal from 'sweetalert2';
import { Component, OnInit, ViewChild, Input, Output, AfterViewInit, EventEmitter } from '@angular/core';
import { DataTable } from 'primeng/components/datatable/datatable';
import { TranslateService } from '@ngx-translate/core';
@Component({
  selector: 'ncloud-fee',
  template: `<div class="panel panel-default">
            <div class="panel-heading">{{('crm.component.fee.heading' | translate) || '费用信息'}}</div>
            <div class="panel-body">
              <p-dataTable #dt [value]="fees" [editable]="true" (onEditInit)="onEditInit($event)" 
                (onEditComplete)="onEditComplete($event)" [rows]="10" [paginator]="true" [pageLinks]="3" 
                [rowsPerPageOptions]="[5,10,20]" [resizableColumns]="true"
                reorderableColumns="true">
                <p-column field="prodName" header="{{('crm.component.fee.dt.prodName' | translate) || '产品'}}" [editable]="false" 
                    [style]="{'width':'20%'}"></p-column>
                <p-column field="discntNameShow" header="{{('crm.component.fee.dt.discntNameShow' | translate) || '套餐'}}" 
                    [editable]="false" [style]="{'width':'35%'}"></p-column>
                <p-column field="standard" header="{{('crm.component.fee.dt.standard' | translate) || '标准费用(元)'}}" 
                    [editable]="false" [style]="{'width':'15%'}"></p-column>
                <p-column field="receiveFee" header="{{('crm.component.fee.dt.receiveFee' | translate) || '应收(元)'}}" 
                    [editable]="true" [style]="{'width':'15%'}"></p-column>
                <p-column field="payFee" header="{{('crm.component.fee.dt.payFee' | translate) || '实收(元)'}}" 
                    [editable]="false" [style]="{'width':'15%'}">
                </p-column>
              </p-dataTable>
              </div>
          </div> `
})
export class FeeComponent implements OnInit, AfterViewInit {
  @Input() fees: any = [];
  @ViewChild('dt') dt: DataTable;
  @Output() public onValid: EventEmitter<any> = new EventEmitter();
  constructor(private translate: TranslateService) { }
  ngOnInit() {

  }
  ngAfterViewInit(): void {
  }
  public reset(): void {
    this.fees = [];
    this.dt.value = [];
  }
  public onEditInit(param: any): void {
    const discnt = param.data;
    switch (discnt.discntType) {
      case '0':
        this.clear(param);
        break;
      case '1':
        this.translate.get('crm.component.fee.dt.editWaring').subscribe((res: string) => {
          swal({
            text: res, showConfirmButton: true
          }).catch(swal.noop);
        });
        break;
      case '2':
        this.translate.get('crm.component.fee.dt.editWaring').subscribe((res: string) => {
          swal({
            text: res, showConfirmButton: true
          }).catch(swal.noop);
        });
        break;
    }
  }
  public onEditCancel(param): void {
  }
  public onEditComplete(param): void {
    param.data['payFee'] = param.data[param['column'].field];
    if (param.data[param['column'].field] && param.data[param['column'].field] !== '' && !isNaN(param.data[param['column'].field])) {
      this.onValid.emit(true);
    } else {
      this.onValid.emit(false);
    }
  }
  private clear(param: any): void {
    const col = param.column;
    if (col['field'] === 'receiveFee') {
      const row = param.data;
      row[col['field']] = '';
    }

  }
}
